package com.jr.service.impl;

import com.jr.mapper.UserMapper;
import com.jr.pojo.User;
import com.jr.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    // 密码加密器
    private BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    @Override
    public User queryByUser(User user) {
        User foundUser = userMapper.selectByUsername(user.getUsername());
        // 临时取消密码加密验证，直接使用明文比较
        if (foundUser != null && user.getPassword().equals(foundUser.getPassword()) && "active".equals(foundUser.getStatus())) {
            return foundUser;
        }
        return null;
    }

    @Override
    public User getUserById(Integer userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public User getUserByUsername(String username) {
        return userMapper.selectByUsername(username);
    }

    @Override
    public User getUserByEmail(String email) {
        return userMapper.selectByEmail(email);
    }

    @Override
    public boolean updateUser(User user) {
        user.setUpdatedAt(LocalDateTime.now());
        return userMapper.updateUser(user) > 0;
    }

    @Override
    public boolean updatePassword(Integer userId, String newPassword) {
        User user = new User();
        user.setUserId(userId);
        user.setPassword(newPassword);
        user.setUpdatedAt(LocalDateTime.now());
        return userMapper.updatePassword(user) > 0;
    }

    @Override
    public List<User> getUsersByType(String userType) {
        return userMapper.selectByUserType(userType);
    }

    @Override
    public List<User> getUsersByStatus(String status) {
        return userMapper.selectByStatus(status);
    }

    @Override
    public boolean deleteUser(Integer userId) {
        // 这里可以实现软删除，将状态设置为inactive
        User user = new User();
        user.setUserId(userId);
        user.setStatus("inactive");
        user.setUpdatedAt(LocalDateTime.now());
        return userMapper.updateUser(user) > 0;
    }

    @Override
    public boolean insertUser(User user) {
        try {
            // 数据验证
            if (user.getUsername() == null || user.getUsername().trim().isEmpty()) {
                throw new IllegalArgumentException("用户名不能为空");
            }
            if (user.getPassword() == null || user.getPassword().trim().isEmpty()) {
                throw new IllegalArgumentException("密码不能为空");
            }
            if (user.getEmail() == null || user.getEmail().trim().isEmpty()) {
                throw new IllegalArgumentException("邮箱不能为空");
            }
            if (user.getUserType() == null || user.getUserType().trim().isEmpty()) {
                throw new IllegalArgumentException("用户类型不能为空");
            }

            // 检查用户类型是否有效
            if (!"pet_owner".equals(user.getUserType()) && 
                !"care_provider".equals(user.getUserType()) && 
                !"platform_admin".equals(user.getUserType())) {
                throw new IllegalArgumentException("无效的用户类型");
            }

            // 加密密码
            // 临时取消密码加密，直接使用明文密码
            // String encryptedPassword = passwordEncoder.encode(user.getPassword());
            // user.setPassword(encryptedPassword);

            // 设置默认值
            if (user.getStatus() == null) {
                user.setStatus("active");
            }
            user.setCreatedAt(LocalDateTime.now());
            user.setUpdatedAt(LocalDateTime.now());

            // 插入用户基础信息
            int result = userMapper.insertUser(user);
            return result > 0;
        } catch (Exception e) {
            System.err.println("用户注册失败: " + e.getMessage());
            return false;
        }
    }
}
